package com.suanfa.bintree;


import com.suanfa.util.ArrayUtils;

/**
 * 最大二叉子树
 */
public class MaxBinTree {
    public static void main(String[] args) {
        int[] arr={ 3,2,1,6,0,5};
        BinNodes binNodes = binTree(arr, 0, arr.length-1);
        ArrayUtils.printNodeF(binNodes);
    }

        public static BinNodes binTree(int[] arr, int left, int right){
            if(left > right){
                return null;
            }
            int max = arr[left];
            int index = left;
            for(int i=left; i<= right; i++){
                if(arr[i] > max){
                    max = arr[i];
                    index = i;
                }
            }
            BinNodes root = new BinNodes(max);
            root.setLeft(binTree(arr, left, index-1));
            root.setRight(binTree(arr, index + 1, right));
            return  root;
        }
}
